﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public struct AssetInfo
{
    public string asset_path;
    public string asset_remote_path;
}

public class LoadAssetBundle
{
    //-------------------------------------------------------------------------
    WWW mLoadAssetWWW;
    Queue<AssetInfo> mMapAssetInfo;
    AssetInfo mCurrentAsset;
    MemoryStream mMR;
    FileStream mSW;
    bool mStreamDisposed;
    float mAlreadRead;

    //-------------------------------------------------------------------------    
    public bool AssetLoadDone { get; private set; }
    public bool IsLoadAsset { get; private set; }
    public string CurrentAssetName { get; private set; }
    public float CurrentWriteProgress { get; private set; }
    public float CurrentLoadProgress { get; private set; }
    public int TotalCount { get; private set; }
    public int CurrentIndex { get; private set; }

    //-------------------------------------------------------------------------
    public LoadAssetBundle()
    {
        mMapAssetInfo = new Queue<AssetInfo>();
        AssetLoadDone = false;
        mStreamDisposed = false;
        IsLoadAsset = true;
        CurrentAssetName = "";
        CurrentWriteProgress = 0f;
        CurrentLoadProgress = 0f;
        TotalCount = 0;
        CurrentIndex = 0;
    }

    //-------------------------------------------------------------------------
    public void update(float elapsed_tm)
    {
        if (mLoadAssetWWW == null && mMapAssetInfo.Count > 0)
        {
            CurrentWriteProgress = 0;
            mCurrentAsset = mMapAssetInfo.Dequeue();
            mLoadAssetWWW = new WWW(mCurrentAsset.asset_remote_path);
        }
        else if (mLoadAssetWWW == null && mMapAssetInfo.Count == 0)
        {
            AssetLoadDone = true;
        }

        if (mLoadAssetWWW != null)
        {
            CurrentAssetName = mCurrentAsset.asset_path.Substring(mCurrentAsset.asset_path.LastIndexOf("/"));
            if (mLoadAssetWWW.isDone)
            {
                IsLoadAsset = false;
                CurrentIndex = TotalCount - mMapAssetInfo.Count;
                if (string.IsNullOrEmpty(mLoadAssetWWW.error))
                {
                    float already_read = 0;
                    byte[] bytes = mLoadAssetWWW.bytes;
                    byte[] buffer = new byte[1024 * 1024];

                    string path = "";
                    //string path = MbMain.Instance.PathMgr.combinePersistentDataPath(mCurrentAsset.asset_path);
                    //string path_dir = path.Substring(0, path.LastIndexOf("/"));
                    //if (!Directory.Exists(path_dir))
                    //{
                    //    Directory.CreateDirectory(path_dir);
                    //}

                    using (MemoryStream ms = new MemoryStream(bytes))
                    {
                        int read_length = ms.Read(buffer, 0, buffer.Length);
                        using (FileStream fs = new FileStream(path, FileMode.Create))
                        {
                            //_writeFile(ms, fs, 0, bytes.Length);
                            if (read_length > 0)
                            {
                                already_read += read_length;
                                CurrentWriteProgress = already_read / bytes.Length;
                                fs.BeginWrite(buffer, 0, read_length,
                                (IAsyncResult result) =>
                                {
                                    if (result.IsCompleted)
                                    {
                                        read_length = ms.Read(buffer, 0, buffer.Length);
                                    }
                                },
                                null);
                            }

                            mLoadAssetWWW = null;
                        }
                    }
                }
                else
                {
                    Debug.Log(mLoadAssetWWW.error);
                    mLoadAssetWWW = null;
                    CurrentLoadProgress = 0;
                }
            }
            else
            {
                IsLoadAsset = true;
                CurrentLoadProgress = mLoadAssetWWW.progress;
            }
        }
    }

    //-------------------------------------------------------------------------
    public void setNeedLoadAsset(string asset_local_path, string asset_remote_path)
    {
        AssetInfo asset_info;
        asset_info.asset_path = asset_local_path;
        asset_info.asset_remote_path = asset_remote_path;
        if (!mMapAssetInfo.Contains(asset_info))
        {
            mMapAssetInfo.Enqueue(asset_info);
        }
        TotalCount = mMapAssetInfo.Count;
    }
}
